<?php
/**
 * 
 * FileHandler class
 * 
 * @author		Stijn Van Minnebruggen
 * @version		1.0.0
 * 
 */

class FileHandler
{
	
	
	/**
	 * Upload a file
	 * 
	 * @param	array	$file
	 * @param	string	$targetPath
	 * @param	array	$allowedExt
	 * @param	string	$newName
	 * @param	int		$allowedFileSize
	 * @return	string
	 */
	
	public function upload($file, $targetPath, $allowedExt=false, $newName=false, $allowedFileSize=1048576)
	{
		// required
			global $cosa, $modules, $lang;
			if(!is_dir($targetPath)) {
				if(!@mkdir($targetPath, 0775)) {
					$modules->addMessage($lang->get('file-mkdir-failed'));
					return false;
				}
			}
			if($targetPath{strlen($targetPath)} != '/') $targetPath .= '/';
		
		// upload file
			if(is_uploaded_file($file['tmp_name'])) {
				
				// check extention
					$ext = $this->getExt($file['name']);
					if($allowedExt && is_array($allowedExt) && !in_array($ext, $allowedExt)) {
						$modules->addMessage($lang->get('file-upload-ext-failed'));
						return false;
					}
				
				// check filesize
					$fs = filesize($file['tmp_name']);
					if($fs > $allowedFileSize) {
						$modules->addMessage($lang->get('file-upload-filesize-failed').' ('.$fs.' bytes)');
						return false;
					}
				
				// get or set new name
					$orName = $newName;
					if($newName && !empty($newName)) $newName = $newName.'.'.$ext;
					else $newName = $file['name'];
				
				// check if file exists
					$i = 1;
					while(file_exists($targetPath.$newName)) {
						$add = '_'.$i++.'.'.$ext;
						if($orName && !empty($orName)) $newName = $newName.$add;
						else $newName = $this->getName($file['name']).$add;
					}
				
				// save file
					$fullPath = $targetPath.$newName;
					if(!@copy($file['tmp_name'], $fullPath)) {
						$modules->addMessage($lang->get('file-upload-copy-failed'));
						return false;
					}
					chmod($fullPath, 0775);
				
				// return filename
					$modules->addMessage($lang->get('file-upload-success'));
					return $newName;
				
			} else {
				
				//$modules->addMessage($lang->get('file-upload-failed'));
				//return false;
				
			}
		
	}
	
	
	/**
	 * Copy a file
	 * 
	 * @param	string	$original
	 * @param	string	$new
	 * @param	int		$permissions
	 * @return	bool
	 */
	
	public function copy($original, $new, $permissions='0775')
	{
		global $cosa, $modules, $lang;
		if(!is_file($original)) {
			$modules->addMessage($lang->get('file-copy-notexists'));
			return false;
		}
		if(!@copy($original, $new)) {
			$modules->addMessage($lang->get('file-copy-failed'));
			return false;
		}
		chmod($new, 0775); //$permissions);
		return true;
	}
	
	
	/**
	 * Resize an image
	 * 
	 * @param	string	$image
	 * @param	int		$maxW
	 * @param	int		$maxH
	 * @param	int		$quality
	 * @return	bool
	 */
	
	public function resize($image, $maxW, $maxH, $quality=90)
	{
		// required
			global $cosa, $modules, $lang;
			if(!is_file($image)) {
				$modules->addMessage($lang->get('file-resize-notexists'));
				return false;
			}
		
		// check extension
			$ext = $this->getExt($image);
			if(!in_array($ext, array('jpg', 'jpeg', 'gif', 'png'))) {
				$modules->addMessage($lang->get('file-resize-ext-failed'));
				return false;
			}
		
		// resize image
			$resized = false;
			if($ext == 'jpg' || $ext == 'jpeg') $resized = $this->_resizeJpg($image, $maxW, $maxH, $quality);
			if($ext == 'gif') $resized = $this->_resizeGif($image, $maxW, $maxH, $quality);
			if($ext == 'png') $resized = $this->_resizePng($image, $maxW, $maxH, $quality);
		
		// return result
			if(!$resized) {
				$modules->addMessage($lang->get('file-resize-failed'));
				return false;
			}
			return true;
		
	}
	
	
	/**
	 * Resize a jpg image
	 * 
	 * @param	string	$image
	 * @param	int		$maxW
	 * @param	int		$maxH
	 * @param	int		$quality
	 * @return	bool
	 */
	
	private function _resizeJpg($image, $maxW, $maxH, $quality)
	{
		// required
			global $cosa, $modules, $lang;
		
		// resize
			$o = imagecreatefromjpeg($image);
			$origW = imagesx($o);
			$origH = imagesy($o);
			if($origW > $maxW || $origH > $maxH) {
				$co = $origW / $origH;
				if ($co > 1) { $maxH = $maxW / $co; // landscape
				} else { $maxW = $maxH * $co; // portrait
				}
				$n = ImageCreateTrueColor($maxW, $maxH);
				imagecopyresampled($n,$o,0,0,0,0,$maxW,$maxH,$origW,$origH);
				imagejpeg($n, $image, $quality);
				return true;
			}
			return true;
		
	}
	
	
	/**
	 * Resize a png image
	 * 
	 * @param	string	$image
	 * @param	int		$maxW
	 * @param	int		$maxH
	 * @param	int		$quality
	 * @return	bool
	 */
	
	private function _resizePng($image, $maxW, $maxH, $quality)
	{
		// required
			global $cosa, $modules, $lang;
		
		// resize
			$o = imagecreatefrompng($image);
			$origW = imagesx($o);
			$origH = imagesy($o);
			if($origW > $maxW || $origH > $maxH) {
				$co = $origW / $origH;
				if ($co > 1) { $maxH = $maxW / $co; // landscape
				} else { $maxW = $maxH * $co; // portrait
				}
				$n = ImageCreateTrueColor($maxW, $maxH);
				imagecopyresampled($n,$o,0,0,0,0,$maxW,$maxH,$origW,$origH);
				imagepng($n, $image, $quality);
				return true;
			}
			return true;
		
	}
	
	
	/**
	 * Resize a gif image
	 * 
	 * @param	string	$image
	 * @param	int		$maxW
	 * @param	int		$maxH
	 * @param	int		$quality
	 * @return	bool
	 */
	
	private function _resizeGif($image, $maxW, $maxH, $quality)
	{
		// required
			global $cosa, $modules, $lang;
		
		// resize
			$o = imagecreatefromgif($image);
			$origW = imagesx($o);
			$origH = imagesy($o);
			if($origW > $maxW || $origH > $maxH) {
				$co = $origW / $origH;
				if ($co > 1) { $maxH = $maxW / $co; // landscape
				} else { $maxW = $maxH * $co; // portrait
				}
				$n = ImageCreateTrueColor($maxW, $maxH);
				imagecopyresampled($n,$o,0,0,0,0,$maxW,$maxH,$origW,$origH);
				imagegif($n, $image, $quality);
				return true;
			}
			return true;
		
	}
	
	
	/**
	 * Get file extention
	 * 
	 * @param	string	$fileName
	 * @return	string
	 */
	
	public function getExt($fileName)
	{
		if(empty($fileName) || !$fileName) return false;
		$ext = split('\.', $fileName);
		$ext = array_reverse($ext);
		$ext = $ext[0];
		return $ext;
	}
	
	
	/**
	 * Get file name without extension
	 * 
	 * @param	string	$fileName
	 * @return	string
	 */
	
	public function getName($fileName)
	{
		if(empty($fileName) || !$fileName) return false;
		$name = split('\.', $fileName);
		$name = $name[0];
		return $name;
	}

}

?>